#ifndef ASYNCPUBLTOOLS_H
#define ASYNCPUBLTOOLS_H

/*******************************************************************************
 * Copyright (c) 2013 Frank Pagliughi <fpagliughi@mindspring.com>
 *
 * All rights reserved. This program and the accompanying materials
 * are made available under the terms of the Eclipse Public License v1.0
 * and Eclipse Distribution License v1.0 which accompany this distribution.
 *
 * The Eclipse Public License is available at
 *    http://www.eclipse.org/legal/epl-v10.html
 * and the Eclipse Distribution License is available at
 *   http://www.eclipse.org/org/documents/edl-v10.php.
 *
 * Contributors:
 *    Frank Pagliughi - initial implementation and documentation
 *
 * Changed by
 *    Vladimir Zakharchenko
 *******************************************************************************/

#include <iostream>
#include <cstdlib>
#include <string>
#include <cstring>
#include <cctype>
#include <thread>
#include <chrono>
#include <libmqttcpp/async_client.h>
#include <unistd.h>

class AsyncSubscribe;

namespace async_subscribe_tools
{

class action_listener : public virtual mqtt::iaction_listener
{
    std::string name_;

    virtual void on_failure(const mqtt::itoken& tok);
    virtual void on_success(const mqtt::itoken& tok) ;

public:
    action_listener(const std::string& name);
};

/////////////////////////////////////////////////////////////////////////////


class callback : public virtual mqtt::callback,
        public virtual mqtt::iaction_listener

{
    AsyncSubscribe* m_reciever;
    int nretry_;
    mqtt::async_client& cli_;
    action_listener& listener_;

    void reconnect() ;

    // Re-connection failure
    virtual void on_failure(const mqtt::itoken& tok);

    // Re-connection success
    virtual void on_success(const mqtt::itoken& tok);

    virtual void connection_lost(const std::string& cause);
    virtual void message_arrived(const std::string& topic, mqtt::message_ptr msg);
    virtual void delivery_complete(mqtt::idelivery_token_ptr token);

public:
    callback(mqtt::async_client& cli, action_listener& listener);
    void initSubscribe(AsyncSubscribe*  client);
};

}

#endif

